$(function () {
    var loading = false;
    //分页允许返回的最大条数
    var maxItems = 999;
    //一页返回的最大条数
    var pageSize = 4;
    //获取评论以及回复
    var commentUrl = '/comment/getCommentChildList';
    //发表评论
    var postUrl = '/comment/postComment';
    //页码
    var pageNum = 1;
    //从地址栏URL里尝试获取topicId
    var topicId = getQueryString('topicId');
    var commentId = getQueryString('commentId');
    //是否已添加了评论
    if ($('#comment-num').text()==''){
        $('#comment-num').text(getQueryString('num'));
    }


    addCommentReply(pageSize,pageNum);

    function addCommentReply(pageSize,pageIndex) {
        //拼接出查询的url,赋空值默认就去掉这个条件限制,有值就代表按这个条件去筛选
        var url = commentUrl+'?pageIndex='+pageIndex+'&pageSize='
            +pageSize+'&commentId='+commentId;
        //设定加载符,若还在后台取数据则不能再次访问后台,避免多次重复加载
        loading = true;
        $.getJSON(url,function (data) {
           if (data.success){
               maxItems = data.count;
               $('#headImg').attr('src',data.data.personInfo.headImg);
               $('.facebook-name').html(data.data.personInfo.username);
               $('.icon-clock').html(new Date(data.data.createTime).Format("yyyy-MM-dd"));
               $('#p-content').html(data.data.content);
               var html = '';
               data.data.commentList.map(function (item,index) {
                   html +='<li class="card" data-comment-id="'+item.topicCommentId+'">'+'<div class="card-header no-border">'
                       +'<div class="facebook-avatar">'+'<img src="'+getContextPath()+item.personInfo.headImg+'" width="34" height="34">'
                       +'</div>'+'<div class="facebook-name">'+item.personInfo.username+'</div>'+'<div class="facebook-date">'
                       +'<span class="icon icon-clock">'+new Date(item.createTime).Format("yyyy-MM-dd")+'</span>'
                       +'</div></div>'+'<div class="card-content">'+'<div class="content-padded">'
                       +'<p>'+item.content+'</p>'+'</div></div></li>';
               });
               $('#comment_reply').append(html);
               //获取目前为止已显示的卡片总数,包含之前已经加载的
               var total = $('.list-block .card').length;
               //若总数达到跟按照此条件查询列出来的总数一致,则停止后天的加载
               if(total>=maxItems){
                   $('.infinite-scroll-preloader').css('display','none');
               }else {
                   $('.infinite-scroll-preloader').css('display','block');
               }
               //否则页码加1,继续load出新的店铺
               pageNum +=1;
               //加载结束,可以再次加载了
               loading = false;
               //刷新页面,显示新加载的店铺
               $.refreshScroller();
           }else {
               $.toast(data.errMsg);
           }
        });
    }

    //下滑屏幕自动进行分页搜索
    $(document).on('infinite','.infinite-scroll-bottom',function () {
        if (loading)
            return;
        addCommentReply(pageSize,pageNum);
    });

    //点击显示评论区
    var discussinput = false;
    $('.icon-edit').click(function () {
        if (!discussinput){
            $('#reply_input').css('display','block');
            discussinput=true;
        } else {
            discussinput=false;
            $('#reply_input').css('display','none');
        }
    });
    //发表评论的内容
    $('#submit').click(function () {
        var content = $('#reply-content').val();
        var url = postUrl+'?type=false&topicId='+topicId+'&commentId='+commentId+'&content='+content;
        $.ajax({
            url: url,
            type:"POST",
            contentType:false,
            processData:false,
            cache:false,
            beforeSend: function(request){
                request.setRequestHeader("Token",localStorage.getItem('token'));
            },
            success:function (data) {
                $.toast(data.message);
                if (data.success){
                    //重置页面
                    $('#comment_reply').empty();
                    pageNum=1;
                    addCommentReply(pageSize,pageNum);
                }
            }
        });
    });


    //点击返回原来的页面
    $('.icon-left').click(function () {
       window.location.href = "/topic/topicContent?topicId="+topicId;
    });

    $.init();
});



















